package com.example.demo.service;

import com.example.demo.oldTeacher.old;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

@Service
public class oldTeacherSerivce implements ApplicationContextAware {

    private ArrayList<old> oldTeachers;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, old> beansOfType = applicationContext.getBeansOfType(old.class);
        Set<Map.Entry<String, old>> entries = beansOfType.entrySet();
        for (Map.Entry<String, old> entry : entries) {
            getOldTeachers().add(entry.getValue());
        }

    }

    public void doAction(){
        for (old old :oldTeachers) {
            old.action();
        }
    }

    synchronized ArrayList<old> getOldTeachers() {
        if (oldTeachers==null){
            oldTeachers=new ArrayList<>();
        }
        return oldTeachers;
    }

    /*public void another(){
        ApplicationContext ac =new StaticApplicationContext();
        ac.getBeansOfType(old.class);
    }*/
}
